package com.dr.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dr.common.ValueUtil;
import com.dr.pojo.Invoice;
import com.dr.pojo.Regist;
import com.dr.pojo.Register;
import com.dr.pojo.Scheduling;
import com.dr.pojo.User;
import com.dr.service.InvoiceService;
import com.dr.service.RegistService;
import com.dr.service.RegisterService;
import com.dr.service.SchedulingService;
//@RestController 
@Controller
@RequestMapping("/rg")
public class RegisterController { 
	@Autowired
	private InvoiceService invoiceService;
	
	@Autowired
	private RegistService registService; 
	
	@Autowired
	private RegisterService registerService; 
	
	@Autowired
	private SchedulingService schedulingService; 
	
//	//加载挂号页面
	@RequestMapping("/regist")
	public String regist(Model md, Register register) {
		//发票号
		Invoice invoice = invoiceService.nextInvoice();
		if(invoice!=null) {
			md.addAttribute("invoice",invoice);
		}
		else {
			md.addAttribute("invoice",new Invoice());
			md.addAttribute("errInvoice","发票已用完，请重新申请");
		}
		//病历号
		String caseNumber = registerService.nextCaseNumber();//病历号
		if(register.getCaseNumber()==null) {//未查到数据
			md.addAttribute("caseNumber",caseNumber);//新病历号
		}else { //查到数据
			md.addAttribute("caseNumber",register.getCaseNumber());//旧病历号
		}
		
		//挂号级别
		List<Regist> registList = registService.findRegistList();
		if(registList.size()!=0) {
			md.addAttribute("registList",registList);
		}
		else {
			md.addAttribute("registList",new ArrayList<>());
			md.addAttribute("errRegist","获取挂号级别信息失败");
		}
		return "/regist/regist.html";
	}	
	
	//查询病历信息
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md, String caseNumber) {
		System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
		System.out.println(caseNumber);
		if(ValueUtil.isEmpty(caseNumber)) { //判断字符串参数caseNumber是否为空
			caseNumber="";
		}
		//根据病历号查询病历信息
		Register register=registerService.findRegisterByCaseNumber(caseNumber);
		System.out.println(register);
		if(register==null) {
			register=new Register();
		}
		else {
			register.setRegistLeID(0);//查询病历信息时，不需要查询医生用的挂号级别
		}
		md.addAttribute("register",register);
		return regist(md, register);
	}	
	
	//查询排班医生
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md, Register register) {
		System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSs");
		System.out.println(register);
		List<Scheduling> schedulingList=schedulingService.findSchedulingList(register);
		md.addAttribute("schedulingList",schedulingList);
		md.addAttribute("register",register);
		return regist(md, register==null?new Register():register);
	}	
		
	//执行挂号
		@RequestMapping("/doRegist")
		public String doRegist(Register register, Model md,HttpSession session) {
			System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYy");
			User loginUser=(User) session.getAttribute("loginUser");
			register.setRegisterID(loginUser.getId());
			System.out.println(register);
			int count=registerService.insertRegister(register);     
			if(count==0) {
				md.addAttribute("errMsg","挂号失败，请联系系统管理员");
			}
			else {
				invoiceService.updateInvoice(register);
			}	
			return "regist/fee.html";
		}	

}











